/*
 * EditProfile.java
 *
 * Created on February 25, 2007, 4:37 PM
 */

package d20chat;

import java.io.*;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.*;       // Used for JFileChooser
import java.awt.*;
import java.awt.event.*;

/**
 * Allows a user to create a new profile.
 */
public class CreateProfile extends javax.swing.JFrame {
    
    private D20Chat tempMain;
    /**
     * Current instance of ProfileManager.
     * Used to fetch variables such as directories.
     */
    public ProfileManager profileManager;
    
    /** Creates new form EditProfile */
/**
     * CreateProfile constructor.
     * Constructor for creat profile class. Displays a window that allows the user
     * to enter information to be saved in a profile.
     */
    public CreateProfile() {
        initComponents();
        
        // Center Create Profile jFrame
        Dimension scrn = getToolkit().getScreenSize();
        this.setBounds( (scrn.width-getWidth())/2, (scrn.height-getHeight())/2, getWidth(), getHeight() );
    }
    
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
/**
     * 
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        lblUsername = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        lblName = new javax.swing.JLabel();
        name = new javax.swing.JTextField();
        lblEmail = new javax.swing.JLabel();
        email = new javax.swing.JTextField();
        lblPhone = new javax.swing.JLabel();
        phone = new javax.swing.JTextField();
        lblWebsite = new javax.swing.JLabel();
        website = new javax.swing.JTextField();
        lblAboutMe = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        aboutMe = new javax.swing.JTextArea();
        visible = new javax.swing.JCheckBox();
        save = new javax.swing.JButton();
        cancel = new javax.swing.JButton();

        setTitle("Create New Profile");
        setLocationByPlatform(true);
        setResizable(false);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                formKeyReleased(evt);
            }
        });

        lblUsername.setForeground(java.awt.Color.darkGray);
        lblUsername.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblUsername.setText("Username:");

        lblName.setForeground(java.awt.Color.darkGray);
        lblName.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblName.setText("Name:");

        lblEmail.setForeground(java.awt.Color.darkGray);
        lblEmail.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblEmail.setText("Email:");

        lblPhone.setForeground(java.awt.Color.darkGray);
        lblPhone.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblPhone.setText("Phone:");

        lblWebsite.setForeground(java.awt.Color.darkGray);
        lblWebsite.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblWebsite.setText("Website:");

        lblAboutMe.setForeground(java.awt.Color.darkGray);
        lblAboutMe.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblAboutMe.setText("About Me:");

        aboutMe.setColumns(20);
        aboutMe.setLineWrap(true);
        aboutMe.setRows(5);
        aboutMe.setWrapStyleWord(true);
        jScrollPane1.setViewportView(aboutMe);

        visible.setForeground(java.awt.Color.darkGray);
        visible.setText("Make my profile visible to others");
        visible.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
        visible.setMargin(new java.awt.Insets(0, 0, 0, 0));

        save.setText("Save");
        save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });

        cancel.setText("Cancel");
        cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAboutMe)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(lblWebsite)
                            .addComponent(lblPhone)
                            .addComponent(lblEmail)
                            .addComponent(lblUsername)
                            .addComponent(lblName))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(website, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                                    .addComponent(email, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)
                                    .addComponent(phone, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(name, javax.swing.GroupLayout.DEFAULT_SIZE, 244, Short.MAX_VALUE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
                    .addComponent(visible)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(save)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 178, Short.MAX_VALUE)
                        .addComponent(cancel)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblUsername)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblName))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblEmail))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblPhone)
                    .addComponent(phone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(website, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblWebsite))
                .addGap(16, 16, 16)
                .addComponent(lblAboutMe)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(15, 15, 15)
                .addComponent(visible)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(save)
                    .addComponent(cancel))
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyReleased
        // Need this for EVERY object on the jFrame
        if (evt.getKeyCode() == KeyEvent.VK_ESCAPE)
            this.setVisible(false);
    }//GEN-LAST:event_formKeyReleased

    
/********************************************************************************
  Method: saveActionPerformed
  Description: Saves the newly created profile to file and adds it to the 
               profileList file
  Parameters: evt
  Used by: CreateProfile.java
  Uses: ProfileManager.addProfile
********************************************************************************/
    private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        // Save all profile information
        String username = this.username.getText();
        String name = this.name.getText();
        String email = this.email.getText();
        String phone = this.phone.getText();
        String website = this.website.getText();
        String aboutMe = this.aboutMe.getText();
        boolean visible = this.visible.isSelected();
        
        // Save the profile to file
        String saveLocation = profileManager.profileDir + username + ".txt";

        FileOutputStream out; // declare a file output object
        PrintStream p; // declare a print stream object

        try {
            // Check if profile (username) is already added
            // Create a buffer for our file to read into
            BufferedReader bin = new BufferedReader(new FileReader(profileManager.profilePath));
            
            boolean profileAdded = false;            
            String line = bin.readLine();
            while (line != null) {
                // Check if currently read in profile is same as
                //  currently added profile
                if (line.compareTo(username) == 0) {
                    profileAdded = true;
                }
                
                // Read in next profile
                line = bin.readLine();
            }
            
            // Done checking username to profile list, so close out file
            bin.close();
            
            if (!profileAdded) {
                // Profile does not already exist         
                // Create a new file output stream
                //  connected to "saveLocation" path
                out = new FileOutputStream(saveLocation);

                // Connect print stream to the output stream
                p = new PrintStream(out);

                // Write profile information to file
                p.println (username);
                p.println (name);
                p.println (email);
                p.println (phone);
                p.println (website);
                p.println (aboutMe);
                p.println (visible);

                p.close();

                // Add new profile to profileList in Profile Manager
                profileManager.addProfile(username);

                // Done with saving profile to file, hide window
                this.setVisible(false);
            } else {
                String message = "Profile " + username + " already exists!\n";
                message = message + "Either edit " + username;
                message = message + " or enter a different username.";
                JOptionPane.showMessageDialog(null, message, "Profile Manager", JOptionPane.WARNING_MESSAGE);
            }
        }
        catch (Exception e)
        {
            System.err.println (e.getMessage());
        }
    }//GEN-LAST:event_saveActionPerformed

    
/********************************************************************************
  Method: cancelActionPerformed
  Description: Closes the create profile window
  Parameters: evt
  Used by: CreateProfile.java
  Uses: NONE
********************************************************************************/
    private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        // Hide form
        this.setVisible(false);
    }//GEN-LAST:event_cancelActionPerformed
    

    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextArea aboutMe;
    private javax.swing.JButton cancel;
    private javax.swing.JTextField email;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lblAboutMe;
    private javax.swing.JLabel lblEmail;
    private javax.swing.JLabel lblName;
    private javax.swing.JLabel lblPhone;
    private javax.swing.JLabel lblUsername;
    private javax.swing.JLabel lblWebsite;
    private javax.swing.JTextField name;
    private javax.swing.JTextField phone;
    private javax.swing.JButton save;
    private javax.swing.JTextField username;
    private javax.swing.JCheckBox visible;
    private javax.swing.JTextField website;
    // End of variables declaration//GEN-END:variables
    
}
